package com.yuan.mall1.controller;

import com.lly835.bestpay.config.WxPayConfig;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayResponse;
import com.yuan.mall1.pojo.PayInfo;
import com.yuan.mall1.service.impl.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
//这里是渲染一个网页，不要用@RestController
@Controller
@Slf4j
@RequestMapping("/pay")
public class PayController {
    //按ByType类型注入bean对象
    @Autowired
    private PayService payService;

    @Autowired
    private WxPayConfig wxPayConfig;
    @GetMapping("/create")
    public ModelAndView create(@RequestParam("orderId") String orderId,
                               @RequestParam("amount") BigDecimal amount,
                               @RequestParam("payType") BestPayTypeEnum bestPayTypeEnum){  //返回的是一个网页
     //  PayResponse response= payService.create("123456789123456", BigDecimal.valueOf(0.01));
        //http://localhost:8080/pay/create?orderId=1234567891234&amount=0.01
       PayResponse response= payService.create(orderId, amount,bestPayTypeEnum);   //调用服务层，订单号和金额
        //  这里要呈现一个网页 进行渲染，引入一个依赖freemaker 这里使用了一个模板技术
        //支付方式不同，渲染就不同，WXPAY_NATIVE使用codeUrl,  ALIPAY_PC使用body
        Map<String, String> map = new HashMap<>();
       if(bestPayTypeEnum == BestPayTypeEnum.WXPAY_NATIVE){
           map.put("codeUrl",response.getCodeUrl());
           map.put("orderId",orderId);
           map.put("returnUrl",wxPayConfig.getReturnUrl());
           return new ModelAndView("createForWxNative");

       }else if(bestPayTypeEnum ==BestPayTypeEnum.ALIPAY_PC){
           //http://localhost:8080/pay/create?orderId=1234567891234&amount=0.01&payType=ALIPAY_PC
           map.put("body",response.getBody());
           return new ModelAndView("createForAlipayPc");
       }
        throw new RuntimeException("暂不支持的支付类型");

       // map.put("codeUrl","weixin://wxpay/bizpayurl?Ep579SY");

//        freemaker模板默认的页面后缀是.ftl
       // return new ModelAndView("create");//网页，视图的名字create.ftl
       // return new ModelAndView("createForWxNative",map);//网页，视图的名字create.ftl
    }
    @PostMapping("/notify")
    @ResponseBody
    public String asyncNotify(@RequestBody String notifyData){
       // log.info("notifyData={}",notifyData);
        return payService.asyncNotify(notifyData);

    }
    //订单支付成功后实现页面跳转
    ////返回的PayInfo是个对象，上面也是@Controller注解不是返回json格式的,    不加不报404
    @GetMapping("/queryByOrderId")
    @ResponseBody
    public PayInfo queryByOrderId(@RequestParam("orderId") String orderId){
        return payService.queryByOrderId(orderId);
    }

}
/**
 * <xml>
 *    <appid>wxd898fcb01713c658</appid>
 *    <mch_id>1483469312</mch_id>
 *    <nonce_str>ww6fA1eaFxMgG83l</nonce_str>
 *    <sign>B7D5D7E3E8E8949908F55379533E9791</sign>
 *    <body>3533162-123345</body>
 *    <notify_url>http://127.0.0.1</notify_url>
 *    <out_trade_no>123456789123456</out_trade_no>
 *    <spbill_create_ip>8.8.8.8</spbill_create_ip>
 *    <total_fee>1</total_fee>
 *    <trade_type>NATIVE</trade_type>
 * </xml>
 * 这里获得的CodeUrl是微信支付付款码的一个信息
 */


